<template>
  <el-dialog id="edit-dialog" :title="$t('coupon.title')" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false" width="80% ">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item :label="$t('coupon.list.table.couponName')" prop="couponName">
        <el-input v-model="formValidate.couponName" clearable :placeholder="$t('common.input')" :maxlength="10" show-word-limit />
      </el-form-item>
      <el-form-item :label="$t('coupon.list.setting.description')" prop="couponDescription">
        <el-input
          v-model="formValidate.couponDescription"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          :placeholder="$t('common.input')"
          :maxlength="140"
          show-word-limit
        />
      </el-form-item>
      <el-form-item :label="$t('coupon.list.table.effectiveTime')" prop="time">
        <el-date-picker
          v-model="time"
          type="daterange"
          range-separator="~"
          :start-placeholder="$t('common.startDate')"
          :end-placeholder="$t('common.endDate')"
        />
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="checked">
          {{ $t('coupon.list.setting.collection') }}
          <el-input-number
            v-model="formValidate.expDays"
            :controls="false"
            :max="10000"
            :placeholder="$t('common.input')"
            style="margin-right: 10px"
          />
          <el-select v-model="formValidate.dateType">
            <el-option
              v-for="item in $t('coupon.list.type')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
          {{ $t('coupon.list.setting.effective') }}
        </el-checkbox>
      </el-form-item>
      <el-form-item :label="$t('coupon.list.table.receiveType')" prop="obtainType">
        <el-select v-model="formValidate.obtainType" :placeholder="$t('common.select')" style="width: 100%">
          <el-option
            v-for="item in receiveData"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('coupon.list.setting.activityCrowd')" prop="couponPeoples">
        <el-select v-model="formValidate.couponPeoples" :placeholder="$t('common.select')" style="width: 100%">
          <el-option
            v-for="item in $t('coupon.list.crowd')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('coupon.list.table.offerType')" prop="couponType">
        <el-select v-model="formValidate.couponType" :placeholder="$t('common.select')" style="width: 100%">
          <el-option
            v-for="item in offer"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="formValidate.couponType" :label="$t('coupon.list.setting.preferentialConditions')" prop="coupon">
        <el-row>
          <el-radio v-model="radio" label="1" style=" margin-bottom: 20px;">
            {{ $t('coupon.list.setting.loan') }}
            <el-input-number
              v-model="formValidate.couponCondition"
              :controls="false"
              :max="100000000"
              :placeholder="$t('common.input')"
            />
            {{ $t('common.unit') }},
            <span>{{ $t('coupon.list.setting.interestReduction') }}</span>
            <span>{{ $t('coupon.list.offertips.interestReduction') }}</span>
            <el-input-number
              v-model="radioValue1"
              :controls="false"
              :max="100000000"
              :placeholder="$t('common.input')"
            />
            <span v-if="formValidate.couponType === 1">{{ $t('common.unit') }}</span>
            <span v-if="formValidate.couponType === 2">%</span>
            <span v-if="formValidate.couponType === 3">{{ $t('coupon.list.offertips.dayFree') }}</span>
          </el-radio>
        </el-row>
        <el-row>
          <el-radio v-model="radio" label="2">
            {{ $t('coupon.list.setting.nothreshold') }}
            <span v-if="formValidate.couponType === 1">{{ $t('coupon.list.setting.interestReduction') }}</span>
            <span v-if="formValidate.couponType === 2">{{ $t('coupon.list.offertips.interestReduction') }}</span>
            <el-input-number
              v-model="radioValue2"
              :controls="false"
              :max="100000000"
              :placeholder="$t('common.input')"
            />
            <span v-if="formValidate.couponType === 1">{{ $t('common.unit') }}</span>
            <span v-if="formValidate.couponType === 2">%</span>
            <span v-if="formValidate.couponType === 3">{{ $t('coupon.list.offertips.dayFree') }}</span>
          </el-radio>
        </el-row>
      </el-form-item>
      <el-form-item :label="$t('coupon.list.table.circulation')" prop="couponCirculation">
        <el-input-number
          v-model="formValidate.couponCirculation"
          :controls="false"
          :max="100000000"
          :placeholder="$t('common.input')"
        />
        {{ $t('coupon.list.setting.zhang') }}
      </el-form-item>
      <el-form-item :label="$t('coupon.list.setting.limitedPerPerson')" prop="couponLimit">
        <el-select v-model="formValidate.couponLimit" :placeholder="$t('common.select')">
          <el-option
            v-for="item in 10"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        {{ $t('coupon.list.setting.zhang') }}
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addCoupon, updateCoupon } from '@/api/operation'
import { formatDate } from '@/utils/method'
export default {
  name: 'CouponDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    const validateTime = (rule, value, callback) => {
      if (this.time && this.time.length > 1) {
        callback()
      } else {
        callback(new Error(this.$t('coupon.list.table.effectiveTime') + this.$t('common.null')))
      }
    }
    const validateCoupon = (rule, value, callback) => {
      if (this.radio === '1') {
        if (this.formValidate.couponCondition && this.radioValue1) {
          callback()
        } else {
          callback(new Error(this.$t('coupon.list.setting.preferentialConditions') + this.$t('common.null')))
        }
      } else {
        if (this.radioValue2) {
          callback()
        } else {
          callback(new Error(this.$t('coupon.list.setting.preferentialConditions') + this.$t('common.null')))
        }
      }
    }
    return {
      checked: false,
      radio: '1',
      radioValue1: null,
      radioValue2: null,
      receiveData: JSON.parse(JSON.stringify(this.$t('coupon.list.receiveData'))).splice(1),
      offer: JSON.parse(JSON.stringify(this.$t('coupon.list.offer'))).splice(1),
      submitLoading: false,
      time: null,
      formValidate: {
        id: null,
        couponName: null,
        couponDescription: null,
        effectiveDays: null,
        createTime: null,
        obtainType: null,
        expDays: null,
        couponDays: null,
        couponPeoples: null,
        couponType: null,
        couponCondition: 0,
        couponValue: null,
        couponCirculation: 1,
        dateType: null,
        effectiveDate: null,
        expDate: null,
        couponLimit: 1
      },
      rules: {
        couponName: [{ required: true, message: this.$t('coupon.list.table.couponName') + this.$t('common.null'), trigger: 'blur' }],
        couponDescription: [{ required: true, message: this.$t('coupon.list.setting.description') + this.$t('common.null'), trigger: 'blur' }],
        time: [{ required: true, validator: validateTime, trigger: 'blur' }],
        obtainType: [{ required: true, message: this.$t('coupon.list.table.receiveType') + this.$t('common.null'), trigger: 'blur' }],
        couponPeoples: [{ required: true, message: this.$t('coupon.list.setting.activityCrowd') + this.$t('common.null'), trigger: 'blur' }],
        couponType: [{ required: true, message: this.$t('coupon.list.table.offerType') + this.$t('common.null'), trigger: 'blur' }],
        coupon: [{ required: true, validator: validateCoupon, trigger: 'blur' }],
        couponCirculation: [{ required: true, message: this.$t('coupon.list.table.circulation') + this.$t('common.null'), trigger: 'blur' }],
        couponLimit: [{ required: true, message: this.$t('coupon.list.setting.limitedPerPerson') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.checked = false
      this.radio = '1'
      this.radioValue1 = null
      this.radioValue2 = null
      this.time = null
      this.$emit('change-dialog', 'editDialog', false)
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = this.formValidate
          data.effectiveDate = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
          data.expDate = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
          if (this.radio === '1') {
            if (data.couponType === 3) {
              data.couponDays = this.radioValue1
            } else {
              data.couponValue = this.radioValue1
            }
          } else {
            if (data.couponType === 3) {
              data.couponDays = this.radioValue2
            } else {
              data.couponValue = this.radioValue2
            }
            data.couponCondition = null
          }
          if (data.id) {
            updateCoupon(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addCoupon(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        }
      })
    },
    changeChecked(val) {
      this.checked = val
    },
    changeRadio(val) {
      console.log(val)
      this.radio = val.toString()
    },
    changeRadioValue(data, val) {
      this[data] = val
    },
    setTimeValue(start, end) {
      this.time = [start, end]
    }
  }
}
</script>

<style lang="scss" scoped>
.el-radio{
  .el-input-number{
    margin: 0 10px;
  }
}
</style>
